using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using Lib.Helper;
using MSIACCSL2.CoaServices;
using MSIACCSL2.SupplierServices;
using WcfService1.DTO;

namespace MSIACCSL2.ViewModels
{
    public class SaldoHutangViewModel : ViewModelBase
    {
        private SupplierServicesClient supplierClient;
        private CoaServicesClient coaClient;
        public  CoaDTO _coa;
       
        public SaldoHutangViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                supplierClient = new SupplierServicesClient();
                coaClient = new CoaServicesClient();
            }
            
        }

        private ObservableCollection<SaldoAwalHutang> _SaldoAwalHutangs;
        private ObservableCollection<SupplierDTO> _Suppliers;

        public ObservableCollection<SaldoAwalHutang> SaldoAwalHutangs
        {
            get { return _SaldoAwalHutangs; }
            set
            {
                if (ReferenceEquals(_SaldoAwalHutangs,value) || value==null)return;
                _SaldoAwalHutangs = value;
                FirePropertyChanged("SaldoAwalHutangs");
                LoadSupplier(SaldoAwalHutangs);
               
            }
        }

        public ObservableCollection<SupplierDTO> Suppliers
        {
            get { return _Suppliers; }
            set
            {
                if (ReferenceEquals(_Suppliers, value) || value == null) return;
                _Suppliers = value;
                FirePropertyChanged("Suppliers");
            }
        }


        public void LoadSupplier(ObservableCollection<SaldoAwalHutang> saldoAwalHutangs)
        {
                supplierClient.GetAllSupplierCompleted += supplierClient_GetAllSupplierCompleted;
                supplierClient.GetAllSupplierAsync(saldoAwalHutangs);
        }

        void supplierClient_GetAllSupplierCompleted(object sender, GetAllSupplierCompletedEventArgs e)
        {
            supplierClient.GetAllSupplierCompleted -= supplierClient_GetAllSupplierCompleted;
            Suppliers = e.Result;

            foreach (var saldoAwalHutang in (ObservableCollection<SaldoAwalHutang>)e.UserState)
            {
                saldoAwalHutang.Suppliers = Suppliers;
                if (saldoAwalHutang.Supplier != null)
                    saldoAwalHutang.Supplier =
                        saldoAwalHutang.Suppliers.FirstOrDefault(x => x.SupplierId == saldoAwalHutang.Supplier.SupplierId);
            }
        }

        public void SaveSaldoAwalHutang()
        {
            coaClient.SaveSaldoAwalHutangCompleted += coaClient_SaveSaldoAwalHutangCompleted;
            foreach (var saldoAwalHutang in SaldoAwalHutangs)
                saldoAwalHutang.Coa = _coa;
            coaClient.SaveSaldoAwalHutangAsync(SaldoAwalHutangs);
        }

        void coaClient_SaveSaldoAwalHutangCompleted(object sender, AsyncCompletedEventArgs e)
        {
            coaClient.SaveSaldoAwalHutangCompleted -= coaClient_SaveSaldoAwalHutangCompleted;
            EventAggregator.PublishMessage("SaveSaldoAwalHutangFinished",SaldoAwalHutangs.Sum(x=>x.Saldo));
        }
    }
}